﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using XsClient.Common;
using XsClient.Parsers;

namespace XsClient.GUI
{
    /// <summary>
    /// WinAddBook.xaml 的交互逻辑
    /// </summary>
    public partial class WinAddBook : Window
    {
        public WinAddBook()
        {
            InitializeComponent();
            Init();
        }
        List<XsSource> sources = new List<XsSource>();
        private void Init()
        {
            try
            {
                sources = ResolverFactory.Sources;
                this.cmbSource.ItemsSource = sources;
                this.cmbSource.SelectedIndex = 0;
            }
            catch (Exception)
            {

            }
        }
        private void BtnOk_Click(object sender, RoutedEventArgs e)
        {
            BookInfo bookInfo = new BookInfo();
            bookInfo.Name = txtName.Text.Trim();
            bookInfo.SourceID = 0;
            bookInfo.Author = txtAnhor.Text.Trim();
            bookInfo.BaseUrl = txtUrl.Text.Trim();
            if (sources != null && sources.Count > 0)
            {
                var csindex= cmbSource.SelectedIndex;
                if (csindex >= 0 && csindex < sources.Count)
                    bookInfo.SourceID = sources[csindex].ID;
            }

            if (string.IsNullOrEmpty(bookInfo.BaseUrl) || string.IsNullOrEmpty(bookInfo.Name))
                return;
            AppHelper.Instance.BookList.Books.Add(bookInfo);
            AppHelper.Instance.BookList.Save();
            this.DialogResult = true;
        }

        private void BtnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}
